import $ from '../pages/common';
var dateTimePicker = require('../pages/_common/dateTimePicker.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    timetype: {  // 时间类型
      type: String,
      value: '',
    },
    selectTime: {  // 选中的日期
      type: String,
      value: '',
    },
    dayNum:{  // 距今天数
      type: Number,
      value:'',
    },
    defaultShow:{  // 默认显示
      type: Boolean,
      value:false,
    },
    hideSec:{  // 是否隐藏秒
      type: Boolean,
      value:false,
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    dateTimeArray: null,
    dateTime: null,
    dateArr: null,
    arr: null,
    startYear: $.getYMD().split('-')[0],
    endYear: parseInt($.getYMD().split('-')[0]) + 1
  },
  ready: function () {
    this._init();
  },
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    _init() {
      // 获取完整的年月日 时分秒，以及默认显示的数组
      if(this.data.dayNum){
        var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear,dateTimePicker.GetDateStr(this.data.dayNum),true);
      }else{
        var obj = dateTimePicker.dateTimePicker(this.data.startYear);
      }
      this.data.hideSec && obj.dateTimeArray.pop()
      this.setData({
        dateTime: obj.dateTime,
        dateTimeArray: obj.dateTimeArray,
        arr: obj.dateTime,
        dateArr: obj.dateTimeArray,
        selectTime: this.data.selectTime || '请选择日期'
      });
      if(this.data.defaultShow){
        this.changeDateTime()
      }
    },

    changeDateTime(e) {
      if(this.data.hideSec){
        this.data.selectTime = this.data.dateArr[0][this.data.arr[0]] + '-' + this.data.dateArr[1][this.data.arr[1]] + '-' + this.data.dateArr[2][this.data.arr[2]] + ' ' + this.data.dateArr[3][this.data.arr[3]] + ':' + this.data.dateArr[4][this.data.arr[4]]
      }else{
        this.data.selectTime = this.data.dateArr[0][this.data.arr[0]] + '-' + this.data.dateArr[1][this.data.arr[1]] + '-' + this.data.dateArr[2][this.data.arr[2]] + ' ' + this.data.dateArr[3][this.data.arr[3]] + ':' + this.data.dateArr[4][this.data.arr[4]] + ':' + this.data.dateArr[5][this.data.arr[5]]
      }
      this.setData({
        dateTimeArray: this.data.dateArr,
        dateTime: this.data.arr,
        selectTime: this.data.selectTime
      });
      this.triggerEvent("run", { time: this.data.selectTime, timetype: this.data.timetype })
    },
    changeDateTimeColumn(e) {
      let arr = this.data.dateTime, dateArr = this.data.dateTimeArray;
      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
      this.setData({
        dateArr: dateArr,
        arr: arr,
      });
    },
  }
})

